home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cgazv5n5.arc / LISTIT.CPP < prev    next >
C/C++ Source or Header  |  1991-09-23  |  3KB  |  86 lines

  1. //--- LISTIT.CPP ------------------------- Listing 3 -----------
  2. // Parsing a list from disk; saving it to disk
  3. // See Listing 1 for copyright terms.
  4. //--------------------------------------------------------------
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include "collect.h"
  10.  
  11. inline void error (char* msg1 = "", char* msg2 = "", 
  12.                    char* msg3 = "") {
  13.   fprintf( stderr, "listit error: %s %s %s\n", 
  14.            msg1, msg2, msg3 );
  15.   exit ( 1 );
  16. }
  17.  
  18. main(int argc, char ** argv) 
  19. {
  20.   if( argc < 3 ) 
  21.       error("usage: listit sourcefile destinationfile");
  22.  
  23.   FILE* infile = fopen(argv[1], "r");
  24.  
  25.   if ( !infile ) 
  26.       error("database file", argv[1], "not found");
  27.  
  28.   // Build recordArray by parsing database file:
  29.  
  30.   recordArray* items = new recordArray;
  31.   const size = 500; 
  32.   char line[size];
  33.  
  34.   while ( fgets ( line, size, infile )) {  // get lines 'til EOF
  35.     const char* quote = "\"";  // comma-separated ASCII file
  36.     char * name = strtok ( line, quote ); // ANSI C function
  37.  
  38.     // throw away the intervening comma and spaces
  39.     strtok ( 0, quote );  
  40.     char * desc = strtok ( 0, quote );  // get next token in line
  41.     items->add ( new record ( name, desc ));
  42.   }
  43.   fclose ( infile );
  44.  
  45.   // Display and modify the list:
  46.   for ( int quitflag = 0; !quitflag; ) {
  47.     // display list:
  48.     for( int I = 0; I < items->arraySize(); I++ )
  49.       printf( "[%d]%s : \"%s\"\n", I,
  50.               items->rec(I).Name(), items->rec(I).Description());
  51.     printf( "(A)dd item, (D)elete item, (Q)uit " );
  52.  
  53.     switch ( getch() ) {
  54.       case 'A': case 'a' :
  55.         char name[NameSize], description[DescriptionSize];
  56.         printf( "\n name: " );        gets( name );
  57.         printf( "\n description: " ); gets( description );
  58.         items->add( new record ( name, description ));
  59.         break;
  60.       case 'D': case 'd' :
  61.         printf( "item number to delete? " );
  62.         int index = atoi ( gets ( line ));
  63.         if ( index < 0 || index >= items->arraySize())
  64.            error( "delete index out of range" );
  65.         items->destroy ( index );
  66.      // create a new one with the copy constructor
  67.      // to automatically remove the null spots:
  68.         recordArray* temp = items;
  69.         items = new recordArray ( *temp );
  70.         delete temp;
  71.         break;
  72.       case 'Q': case 'q' :
  73.         quitflag++;
  74.         break;
  75.     }
  76.   }
  77.   // Write out the array in simple comma-separated ASCII format:
  78.   printf ( "write to destination file %s? (y/n) ", argv[2] );
  79.   if ( getch() == 'n' ) 
  80.       error ( "Termination without writing file" );
  81.   FILE* outfile = fopen(argv[2], "w");
  82.   for ( int i = 0; i < items->arraySize(); i++ )
  83.     fprintf ( outfile, "\"%s\",\"%s\"\n",
  84.       items->rec(i).Name(), items->rec(i).Description());
  85.   fclose ( outfile );
  86. }